﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BT_Mang_Nhom13.Library;
using BT_Mang_Nhom13.Checker;

namespace BT_Mang_Nhom13.Sorter
{
    public class SelectionSorter<T> :MySorter<T> where T:IComparable<T>
    {
        public override T[] Sort(T[] Data, MyComparer<T> Comparer)
        {
            int min;
            int size = Data.Length;
            for (int i = 0; i < size; i++)
            {
                min = i;
                for (int j = i + 1; j < size; j++)
                {
                    if (Comparer.CompareTo(Data[j], Data[min]) < 0)
                    {
                        min = j;
                    }
                }
                if (Comparer.CompareTo(Data[min], Data[i]) < 0)
                    Swap(Data,i, min);
            }

            return Data;
        }

        public override T[] Sort(T[] Data, Func<T, T, int> Compare)
        {
            int min;
            int size = Data.Length;
            for (int i = 0; i < size; i++)
            {
                min = i;
                for (int j = i + 1; j < size; j++)
                {
                    if (Compare(Data[j], Data[min]) < 0)
                    {
                        min = j;
                    }
                }
                if (Compare(Data[min], Data[i]) < 0)
                    Swap(Data, i, min);
            }

            return Data;
        }
    }
}
